package com.hxy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.common.DateUtil;
import com.hxy.common.JsonResult;
import com.hxy.dao.AddressbookMapper;
import com.hxy.dao.BusinessMapper;
import com.hxy.dao.StuffinfoMapper;
import com.hxy.pojo.Branch;
import com.hxy.pojo.Business;
import com.hxy.pojo.vo.BusinessVo;
import com.hxy.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private StuffinfoMapper stuffinfoMapper;
    @Autowired
    private AddressbookMapper addressbookMapper;

    @Override
    public PageInfo<BusinessVo> selectAllPagerBusinessVo(Integer page, int limit, BusinessVo business) {
        PageHelper.startPage(page, limit);
        List<BusinessVo> BusinessVos = businessMapper.selectAllBusinessVo(business);
        for (BusinessVo businessVo : BusinessVos) {
            if (businessVo.getBeName()==null){
                businessVo.setBeName("未分配");
            }
        }
        PageInfo<BusinessVo> BranchsPageInfo = new PageInfo<>(BusinessVos);
        return BranchsPageInfo;
    }

    @Override
    public List<Business> findBusinessNamesByid(Integer id) {
        List<Business> businessList = businessMapper.selectByBranchId(id);
        return businessList;
    }

    @Override
    public BusinessVo selectInfoByBusines(Integer id) {
        BusinessVo businessVo = businessMapper.selectInfoByBusines(id);

        if (businessVo != null) {
            return businessVo;
        } else {
            return null;
        }
    }

    @Override
    public JsonResult updateOne(BusinessVo business) {

        //更改员工表信息
        int i1 = stuffinfoMapper.updateBranchByBusinessIdByUpdateOne(business.getBranchId(),business.getId());
        //更改通讯录信息
        int i3 = addressbookMapper.updateBranchByBusinessIdByUpdateOne(business.getBranchId(),business.getId());
        //更改bussiness表信息
        int i = businessMapper.updateByPrimaryKey(business);
        System.out.println(business.toString());
        if (i > 0) {
            return new JsonResult(0, "success", null);
        } else {
            return new JsonResult(1, "error", null);
        }
    }

    /**
     * 岗位添加
     * @param business
     * @return
     */
    @Override
    public JsonResult add(BusinessVo business) {
        //添加时间
        String sdate = DateUtil.date2Str(new Date(), DateUtil.DATE_FORMAT_1);
        business.setCreatime(sdate);
        int insert = businessMapper.insert(business);
       if (insert > 0){
           return new JsonResult(0, "success",null);
       }
       else {
           return new JsonResult(1, "fail",null);
       }
    }

    /**
     *岗位删除
     * @param id
     * @return
     */
    @Override
    public JsonResult delete(Integer id) {
        int i = businessMapper.deleteByPrimaryKey(id);
        if (i>0){
        return new JsonResult(0,"success",null);
        }
        else {
            return new JsonResult(1,"fail",null);
        }
    }
    }

